const qiniu = require('qiniu')
const nanoid = require('nanoid')
const config = require('../config')

const bucket = config.qiniu.bucket
const mac = new qiniu.auth.digest.Mac(config.qiniu.AK, config.qiniu.SK)
const cfg = new qiniu.conf.Config()
const client = new qiniu.rs.BucketManager(mac, cfg)

const uploadToQiniu = async(url, key) => {
    return new Promise((resolve, reject) => {
        client.fetch(url, bucket, key, (err, ret, info) => {
            if (err) {
                reject(err)
            } else {
                if (info.statusCode === 200) {
                    resolve({ key })
                } else {
                    reject(info)
                }
            }
        })
    })
}

;(async() => {
    let movies = [{
        video: 'http://vt1.doubanio.com/202001131710/37743ecdf7bb2caba4ee4666f67fd626/view/movie/M/402570449.mp4',
        doubanId: '1292063',
        poster: 'https://img3.doubanio.com/view/photo/l_ratio_poster/public/p2578474613.jpg',
        cover: 'https://img3.doubanio.com/img/trailer/medium/2579911492.jpg?'
    }]

    movies.map(async movie => {
        if (movie.video && !movie.key) {
            try {
                console.log('开始传 video')
                let videoData = await uploadToQiniu(movie.video, nanoid() + '.mp4')
                console.log('开始传 cover')
                let coverData = await uploadToQiniu(movie.cover, nanoid() + '.jpg')
                console.log('开始传 poster')
                let posterData = await uploadToQiniu(movie.poster, nanoid() + '.jpg')

                if (videoData.key) {
                    movie.videoKey = videoData.key
                }
                if (coverData.key) {
                    movie.coverKey = coverData.key
                }
                if (posterData.key) {
                    movie.posterKey = posterData.key
                }

                console.log(movie) 
            } catch (err) {
                console.log(err)
            }
        }
    })
})()


// {
                //     video: 'http://vt1.doubanio.com/202001131710/37743ecdf7bb2caba4ee4666f67fd626/view/movie/M/402570449.mp4',
                //     doubanId: '1292063',
                //     poster: 'https://img3.doubanio.com/view/photo/l_ratio_poster/public/p2578474613.jpg',
                //     cover: 'https://img3.doubanio.com/img/trailer/medium/2579911492.jpg?',
                //     videoKey: 'http://video.iblack7.com//vSeoLKjWtjh6a79X_RUhR.mp4',
                //     coverKey: 'RGDkE7v8-GGYTb7hF-YIF.jpg',
                //     posterKey: 'WpCmkWxI_ARkappaHNpQq.jpg'
                // }